var crypto = require("crypto");

var algorithm = "aes-128-ecb";	//何种加密算法
var key = "15814039458";		//密钥
var iv = "";					//密钥向量
var clearEncoding = "utf8";
var cipherEncoding = "hex";

var EncryptDES = function(data){
	console.log("Original clear text: " + data);
	/*加密*/
	var cipher = crypto.createCipher(algorithm, key, iv);
	var cipherChunks = [];
	cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
	cipherChunks.push(cipher.final(cipherEncoding));
	console.log(cipherEncoding + " ciphertext: " + cipherChunks.join(''));
	return cipherChunks.join('');
}

var DecryptDES = function(encryptedData){
	/*解密*/
	var decipher = crypto.createDecipher(algorithm, key, iv);
	var plainChunks = [];
	
	plainChunks.push(decipher.update(encryptedData, cipherEncoding, clearEncoding));	
	plainChunks.push(decipher.final(clearEncoding));

	console.log(clearEncoding + " plaintext decipher: " + plainChunks.join(''));
	return plainChunks.join('');
}

var MD5 = function(data){
	console.log("Original clear text: " + data);
	//生成口令的散列值
	var md5 = crypto.createHash('md5');	
	return md5.update(data).digest('hex');
}

module.exports.EncryptDES = EncryptDES;
module.exports.DecryptDES = DecryptDES;
module.exports.MD5 = MD5;